package com.ruoyi.algorithm.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.algorithm.entity.OperatorVersion;
import com.ruoyi.algorithm.mapper.OperatorVersionMapper;
import com.beihu.sightplus.common.repository.BaseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OperatorVersionRepository extends BaseRepository<OperatorVersionMapper, OperatorVersion> {

    @Autowired
    private OperatorVersionMapper operatorVersionMapper;

    /**
     * 判断是否有算子关联算子版本
     */
    public Boolean isBindOperator(Long operatorId) {
        boolean isBind = false;
        LambdaQueryWrapper<OperatorVersion> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OperatorVersion::getOperatorId, operatorId).eq(OperatorVersion::getDelFlag, false);
        List<OperatorVersion> operatorVersionList = operatorVersionMapper.selectList(wrapper);
        if (operatorVersionList.size() > 0) {
            isBind = true;
        }

        return isBind;
    }

    /**
     * 根据算子id查询算子版本列表
     *
     * @param operatorId 算子id
     * @return 算子版本列表
     */
    public List<OperatorVersion> listByOperatorId(Long operatorId) {
        LambdaQueryWrapper<OperatorVersion> operatorVersionLQW = Wrappers.lambdaQuery(OperatorVersion.class)
                .eq(OperatorVersion::getOperatorId, operatorId)
                .eq(OperatorVersion::getDelFlag, false);
        return operatorVersionMapper.selectList(operatorVersionLQW);
    }

    /**
     * 新增一条算子版本数据
     *
     * @param operatorVersion 算子版本实体
     */
    public void insertOperatorVersion(OperatorVersion operatorVersion) {
        operatorVersion.setCreateTime(new Date());
        operatorVersion.setUpdateTime(new Date());
        operatorVersionMapper.insert(operatorVersion);
    }

    /**
     * 更新一条算子版本数据
     *
     * @param operatorVersion 算子版本呢
     * @return 是否成功更新一条以上
     */
    public boolean updateByEntity(OperatorVersion operatorVersion) {
        operatorVersion.setUpdateTime(new Date());
        int update = operatorVersionMapper.updateById(operatorVersion);
        return update > 0;
    }
}
